/**
 * @description: 入口文件
 * @version: 0.0.1
 * @author: xiech
 * @Date: 2023-06-12 18:16:39
 */

import { Configuration, App, Inject } from '@midwayjs/core';
import * as koa from '@midwayjs/koa';
import * as validate from '@midwayjs/validate';
import * as jwt from '@midwayjs/jwt';
import * as crossDomain from '@midwayjs/cross-domain';
import * as info from '@midwayjs/info';
import { join } from 'path';
import * as upload from '@midwayjs/upload';
import * as staticFile from '@midwayjs/static-file';
import { ILogger } from '@midwayjs/logger';
import * as axios from '@midwayjs/axios';

// 过滤器
import { DefaultErrorFilter } from './filter/default.filter';
import { NotFoundFilter } from './filter/notfound.filter';
import { ValidateErrorFilter } from './filter/validate.filter';
import { UnauthorizedFilter } from './filter/unauthorized.filter';
// 中间键
import { ReportMiddleware } from './middleware/report.middleware';
import { FormatMiddleware } from './middleware/format.middleware';
import { JwtMiddleware } from './middleware/jwt.middleware';
import { AuthMiddleware } from './middleware/auth.middleware';
// 任务队列
import * as bull from '@midwayjs/bull';
// 本地任务
import * as cron from '@midwayjs/cron';
//redis
import * as redis from '@midwayjs/redis';
//sequelize
import * as sequelize from '@midwayjs/sequelize';
//oss
import * as oss from '@midwayjs/oss';


@Configuration({
  imports: [
    koa,
    validate,
    jwt,
    crossDomain,
    bull,
    cron,
    redis,
    upload,
    sequelize,
    staticFile,
    oss,
    axios,
    {
      component: info,
      enabledEnvironment: ['local'],
    },
  ],
  importConfigs: [join(__dirname, './config')],
})
export class ContainerLifeCycle {
  @App()
  app: koa.Application;

  @Inject()
  logger: ILogger;

  @Inject()
  bullFramework: bull.Framework;

  async onReady() {
    // 添加中间键
    this.app.useMiddleware([ReportMiddleware, FormatMiddleware, JwtMiddleware, AuthMiddleware]);
    // 添加过滤器
    this.app.useFilter([UnauthorizedFilter, NotFoundFilter, DefaultErrorFilter, ValidateErrorFilter]);
  }

  // 服务启动后执行
  async onServerReady() {
    this.logger.info('onServerReady');
  }
}
